package com.fortune.controller;

import java.util.List;

import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.dto.excution.MStoreInExcution;
import com.fortune.dto.excution.ProduceSalePriceExcution;
import com.fortune.dto.in.cProductPriceMgt.CProductPriceListDto;
import com.fortune.dto.in.mStoreIn.MStoreInListDto;
import com.fortune.entity.DUserInfo;
import com.fortune.entity.MUserInfo;
import com.fortune.enums.MStoreInEnum;
import com.fortune.enums.ProduceSalePriceEnum;
import com.fortune.service.DProductWholesalePriceService;
import com.fortune.vo.mStoreIn.MStoreInInfo;
import io.swagger.annotations.ApiImplicitParams;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fortune.base.BaseAPIExcution;
import com.fortune.dto.in.cProductPriceMgt.CProductPriceAddDto;
import com.fortune.dto.in.cProductPriceMgt.CProductPriceEditDto;
import com.fortune.vo.cProductPriceMgt.CProductPrice;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Api(value = "cProductPriceMgt", description="经销商对客户统一批发价管理", tags = {"经销商对客户统一批发价管理"})
@RestController
@RequestMapping("cProductPriceMgt")
@Validated
public class CProductPriceMgtController{
	@Autowired
	private DProductWholesalePriceService productWholesalePriceService;
	
	@ApiOperation(value = "统一批发价列表", nickname = "cProductPriceList", notes = "", tags = {"@卞兴旺"})
	@ApiImplicitParams({
			@ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
			@ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int")
	})
    @GetMapping(value = "list",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<PagingData> list(HttpServletRequest request,@ApiIgnore @Validated CProductPriceListDto cProductPriceListDto) {
		cProductPriceListDto.setPage((cProductPriceListDto.getPage()-1)*10);
		HttpSession session=request.getSession();
		DUserInfo dUserInfo= (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String dealerId=dUserInfo.getdId();
		List<CProductPrice> list = productWholesalePriceService.findWholesale(dealerId,cProductPriceListDto);
		int count = productWholesalePriceService.findWholesaleCount(dealerId);
		PagingData pagingData = new PagingData();
		pagingData.setData(list);
		pagingData.setRecordsTotal(count);
		return new ProduceSalePriceExcution<PagingData>(pagingData, ProduceSalePriceEnum.SUCCESS);
	}

	@ApiOperation(value = "添加统一批发价", nickname = "cProductPriceAdd", notes = "", tags = {"@卞兴旺"})
    @PostMapping(value = "add",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> add(HttpServletRequest request,@Validated @RequestBody CProductPriceAddDto addDto) {
		request.getSession().getAttribute(Constants.SESSION_M_USER);
		HttpSession session=request.getSession();
		DUserInfo dUserInfo= (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String companyId=dUserInfo.getmId();
		String dealerId=dUserInfo.getdId();
		int num =productWholesalePriceService.addProductSalePrice(companyId,dealerId,addDto);
		if (num < 1){
			return new ProduceSalePriceExcution<Void>(null, ProduceSalePriceEnum.FAILURE);
		}
		return new ProduceSalePriceExcution<Void>(null, ProduceSalePriceEnum.SUCCESS);
	}
	
	@ApiOperation(value = "编辑统一批发价", nickname = "cProductPriceEdit", notes = "", tags = {"@卞兴旺"})
    @PutMapping(value = "edit",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> edit(HttpServletRequest request, @Validated @RequestBody CProductPriceEditDto editDto) {
		HttpSession session=request.getSession();
		MUserInfo mUserInfo= (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		DUserInfo dUserInfo= (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String companyId=dUserInfo.getmId();
		String dealerId=dUserInfo.getdId();
		int num =productWholesalePriceService.editProductSalePrice(companyId,dealerId,editDto);
		if (num < 1){
			return new ProduceSalePriceExcution<Void>(null, ProduceSalePriceEnum.FAILURE);
		}
		return new ProduceSalePriceExcution<Void>(null, ProduceSalePriceEnum.SUCCESS);
	}
	
	@ApiOperation(value = "删除统一批发价", nickname = "cProductPriceDelete", notes = "", tags = {"@卞兴旺"})
	@ApiImplicitParam(name="productId", value="商品id",paramType="query",dataType="string")
    @DeleteMapping(value = "delete",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> delete(HttpServletRequest request,@Validated @NotBlank(message="商品id不能为空") String productId) {
		HttpSession session=request.getSession();
		MUserInfo mUserInfo= (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		DUserInfo dUserInfo= (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String companyId=dUserInfo.getmId();
		String dealerId=dUserInfo.getdId();
		int num =productWholesalePriceService.deleteProductSalePrice(companyId,dealerId,productId);
		if (num < 1){
			return new ProduceSalePriceExcution<Void>(null, ProduceSalePriceEnum.FAILURE);
		}
		return new ProduceSalePriceExcution<Void>(null, ProduceSalePriceEnum.SUCCESS);
	}
	
}
